from SystematicFactors.General import *
import os
import Core.Gadget as Gadget
import Core.MySQLDB as MySQLDB

def Calc_Est_HS300(zyyx_database, database, datetime1, datetime2):
    #
    if (datetime2 - datetime1).days < 200: # 180 天半年
        datetime0 = datetime2 - datetime.timedelta(days=200)
    else:
        datetime0 = datetime1

    #
    filter = []
    filter.append(["index_code", "000300"])
    filter.append(["con_date", ">=", datetime0])
    filter.append(["con_date", "<=", datetime2])

    # 指数一致预期滚动数据表
    documents = zyyx_database.Find("goaldb", "con_forecast_roll_idx", filter=filter, sort=[("con_date", 1)])
    df = Gadget.DocumentsToDataFrame(documents)
    df["date_t"] = pd.to_datetime(df["con_date"])
    df.set_index("date_t", inplace=True)
    #
    df = Fix_Trim_Weekly(df, 1, 5)

    # 计算趋势
    df["Est_EPS_HS300_Momentum_1M"] = df["con_eps_roll"] / df["con_eps_roll"].shift(20) - 1
    df["Est_EPS_HS300_Momentum_3M"] = df["con_eps_roll"] / df["con_eps_roll"].shift(60) - 1
    df["Est_EPS_HS300_Momentum_6M"] = df["con_eps_roll"] / df["con_eps_roll"].shift(120) - 1
    # print(df["Est_EPS_HS300_Momentum_6M"])

    # 按照周聚合
    df_by_week = df.resample("W").last()
    df_by_week["Report_Date"] = df_by_week.index
    df_by_week["Release_Date"] = df_by_week["con_date"]

    # peg滚动
    df_by_week["Est_PEG_HS300_Weekly_Chg"] = df_by_week["con_peg_roll"] - df_by_week["con_peg_roll"].shift(1)
    df_by_week["Est_ROE_HS300_Weekly_Chg"] = df_by_week["con_roe_roll"] - df_by_week["con_roe_roll"].shift(1)
    df_by_week["Est_EPS_HS300_Weekly_Chg"] = df_by_week["con_eps_roll"].diff(1)
    #
    df_by_week.dropna(subset=["Est_PEG_HS300_Weekly_Chg", "Est_ROE_HS300_Weekly_Chg"], inplace=True)

    # 确定正确的起始时间
    df_by_week = df_by_week[(df_by_week["Report_Date"] >= datetime1) & (df_by_week["Report_Date"] <= datetime2)]
    # print(df_by_week.tail())
    # print(df_by_week[["Report_Date","Release_Date", "con_eps_roll", "Est_EPS_HS300_Weekly_Chg"]].tail())
    # df_by_week.to_csv("d://test_eps_change.csv")

    #
    Save_Systematic_Factor_To_Database(database, df_by_week, save_name="Est_PEG_HS300", field_name="con_peg_roll")
    Save_Systematic_Factor_To_Database(database, df_by_week, save_name="Est_ROE_HS300", field_name="con_roe_roll")
    Save_Systematic_Factor_To_Database(database, df_by_week, save_name="Est_EPS_HS300", field_name="con_eps_roll")
    Save_Systematic_Factor_To_Database(database, df_by_week, save_name="Est_PEG_HS300_Weekly_Chg")
    Save_Systematic_Factor_To_Database(database, df_by_week, save_name="Est_ROE_HS300_Weekly_Chg")
    Save_Systematic_Factor_To_Database(database, df_by_week, save_name="Est_EPS_HS300_Weekly_Chg")
    #
    Save_Systematic_Factor_To_Database(database, df_by_week, save_name="Est_EPS_HS300_Momentum_1M")
    Save_Systematic_Factor_To_Database(database, df_by_week, save_name="Est_EPS_HS300_Momentum_3M")
    Save_Systematic_Factor_To_Database(database, df_by_week, save_name="Est_EPS_HS300_Momentum_6M")


# 计算分位数，原则上需要依赖所有历史数据
def Calc_Est_Percentile_HS300(zyyx_database, database, datetime1, datetime2):
    #
    datetime0 = datetime.datetime(2000, 1, 1)
    #
    filter = []
    filter.append(["index_code", "000300"])
    # 提取全部数据
    documents = zyyx_database.Find("goaldb", "con_forecast_roll_idx", filter=filter, sort=[("con_date", 1)], projection=["con_date", "con_pb_roll"])
    df = Gadget.DocumentsToDataFrame(documents, keep=["con_date", "con_pb_roll"])

    # 计算历史分位数
    fill_historical_probability_percentile(df, "con_pb_roll")
    print(df.tail())
    # df.to_csv("d://EST_PB.csv")

    # 变频
    df["date_t"] = pd.to_datetime(df["con_date"])
    df.set_index("date_t", inplace=True)
    df_by_week = df.resample("W").last()
    df_by_week["Report_Date"] = df_by_week.index
    df_by_week["Release_Date"] = df_by_week["con_date"]
    df_by_week["Dif"] = df_by_week["con_pb_roll_Rank"] - df_by_week["con_pb_roll_Rank"].shift(1)

    # 确定正确的起始时间
    df_by_week = df_by_week[(df_by_week["Report_Date"] >= datetime1) & (df_by_week["Report_Date"] <= datetime2)]
    # print(df_by_week.tail())

    Save_Systematic_Factor_To_Database(database, df_by_week, save_name="Est_PB_HS300", field_name="con_pb_roll")
    Save_Systematic_Factor_To_Database(database, df_by_week, save_name="Est_PB_HS300_Rank",
                                       field_name="con_pb_roll_Rank")
    Save_Systematic_Factor_To_Database(database, df_by_week, save_name="Est_PB_HS300_Rank_Weekly_Chg", field_name="Dif")


if __name__ == '__main__':
    #
    pathfilename = os.getcwd() + "\..\Config\config2.json"
    config = Config.Config(pathfilename)
    database = config.DataBase("JDMySQL")
    realtime = config.RealTime()
    #
    # ---Connect zhao yang yong xu Database---
    zyyx_database = MySQLDB.MySQLDB("172.25.4.214", "3306",
                                    username="j_goaldb",
                                    password="5JrBqPn9eX8N")
    #
    datetime1 = datetime.datetime(2000, 6, 1)
    datetime2 = datetime.datetime(2020, 8, 9)
    #
    # w.start()
    #
    Calc_Est_HS300(zyyx_database, database, datetime1, datetime2)
    Calc_Est_Percentile_HS300(zyyx_database, database, datetime1, datetime2)